package com.example.ql.express.utils.operate;

import com.example.ql.express.model.enums.AppendRuleEnum;
import com.example.ql.express.model.vo.ScriptConfigVO;

import java.util.Objects;

import static com.example.ql.express.model.constants.SystemCharConstant.ONE_3;

/**
 * @author xuweizhi
 * @since 2021/10/29 16:18
 */
public class ReferenceOperateCompare implements OperateCompare {

    @Override
    public String generateCompareRule(ScriptConfigVO scriptConfig) {
        StringBuilder stringBuilder = new StringBuilder();
        if (Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.FIRST) || Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.ALL)) {
            stringBuilder.append(String.format(ONE_3, scriptConfig.getParentPath() + " != null "));
        }
        // 替换值有问题
        String compareRule = scriptConfig.getCompareRule();
        for (String level : scriptConfig.getLevels()) {
            compareRule = compareRule.replaceFirst("#\\{level}", level + "");
        }
        stringBuilder.append(compareRule);
        if (Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.END) || Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.ALL)) {
            stringBuilder.append(AppendRuleEnum.END.getContent());
        }
        return stringBuilder.toString();
    }
}
